package com.wyh.spring.demo.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:用户登录后端代码
 * User: 王圆豪
 * Date: 2025-07-01
 * Time: 12:58
 */
@RequestMapping("/user")
@RestController
public class UserController {
    /**
     * 校验账号密码是否正确
     * @param userName
     * @param passWard
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Boolean login(String userName, String passWard, HttpSession session){
        //”n*空格“和null是非法的
        if (!StringUtils.hasText(userName) || !StringUtils.hasText(passWard)){
            return false;
        }
        //调用equals方法的时候要让字符串常量在前面，这样不会报空指针异常
        if ("wyh".equals(userName) && "135790".equals(passWard)){
            session.setAttribute("userName", userName);
            return true;
        }
        return false;
    }

    /**
     * 查询当前登录的用户
     */
    @RequestMapping(value = "/getLoginUser",method = RequestMethod.GET)
    public String getLoginUser(HttpServletRequest request){
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("userName");
        return name;
    }

}